package com.service.Impl;

import java.util.Map;
import java.util.Set;

import com.entity.Book;
import com.entity.Cart;
import com.entity.Product;
import com.service.BookService;
import com.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CartServiceImpl implements CartService {
	@Autowired
	private BookService service;
	@Override
	public Cart addCart(Integer bookId,Cart cart) {
		// TODO Auto-generated method stub
//		if(cart==null){
//			cart=new Cart();
//		}
		Map<Integer,Product> addMap = cart.getAddMap();
		if(addMap.containsKey(bookId)){
			Product product=addMap.get(bookId);
			int i=product.getCount()+1;
			product.setCount(i);
		}else{
			Book book=service.showOne(bookId);
			Product product=new Product();
			product.setBook(book);
			product.setCount(1);
			addMap.put(bookId, product);
		}
		Double totalPrice=0.0;
		Double savePrice=0.0;
		Double dangPrice=0.0;
		Set<Map.Entry<Integer,Product>>set=addMap.entrySet();
		for(Map.Entry<Integer, Product> addmap:set){
			totalPrice+=addmap.getValue().getBook().getBookPrice()*addmap.getValue().getCount();
			dangPrice+=addmap.getValue().getBook().getDangPrice()*addmap.getValue().getCount();
		}
		savePrice=totalPrice-dangPrice;
		cart.setSavePrice(savePrice);
		cart.setTotalPrice(dangPrice);
		return cart;
	}

	@Override
	public Cart updateCart(Integer bookId, Integer count,Cart cart) {
		Map<Integer,Product> addMap = cart.getAddMap(); 
		Product product = addMap.get(bookId);
		product.setCount(count);
		Double totalPrice = 0.0;
		Double savePrice = 0.0;
		Double dangPrice = 0.0;
		Set<Map.Entry<Integer, Product>> set = addMap.entrySet();
		for (Map.Entry<Integer, Product> addmap : set) {
			totalPrice += addmap.getValue().getBook().getBookPrice()
					* addmap.getValue().getCount();
			dangPrice += addmap.getValue().getBook().getDangPrice()
					* addmap.getValue().getCount();
		}
		savePrice = totalPrice - dangPrice;
		cart.setSavePrice(savePrice);
		cart.setTotalPrice(dangPrice);
		return cart;
	}

	@Override
	public Cart deleteCart(Integer bookId,Cart cart) {
		Map<Integer,Product> addMap=cart.getAddMap();
		Product product=addMap.get(bookId);
		Map<Integer,Product> deleteMap=cart.getDeleteMap();
		deleteMap.put(bookId, product);
		addMap.remove(bookId);
		Double totalPrice=0.0;
		Double savePrice=0.0;
		Double dangPrice=0.0;
		Set<Map.Entry<Integer, Product>>set=addMap.entrySet();
		for(Map.Entry<Integer, Product> addmap:set){
			totalPrice+=addmap.getValue().getBook().getBookPrice()*addmap.getValue().getCount();
			dangPrice+=addmap.getValue().getBook().getDangPrice()*addmap.getValue().getCount();
		}
		savePrice=totalPrice-dangPrice;
		cart.setSavePrice(savePrice);
		cart.setTotalPrice(dangPrice);
		return cart;
	}

	@Override
	public Cart recoverCart(Integer bookId, Cart cart) {
		Map<Integer,Product> deleteMap = cart.getDeleteMap();
		Map<Integer,Product> addMap = cart.getAddMap();
		Product product = deleteMap.get(bookId);

		if(addMap.containsKey(bookId)){
			Product product1 = addMap.get(bookId);
			Integer bookCount = product1.getCount()+product.getCount();
			product1.setCount(bookCount);
			addMap.put(bookId, product1);
		}else{
			Product pro = new Product();
			Book book = service.showOne(bookId);
			pro.setBook(book);
			pro.setCount(pro.getCount());
			addMap.put(bookId, pro);
		}
		deleteMap.remove(bookId);
		Double totalPrice = 0.0;
		Double savePrice = 0.0;
		Double dangPrice = 0.0;
		Set<Map.Entry<Integer, Product>> set = addMap.entrySet();
		for (Map.Entry<Integer, Product> addmap : set) {
			if(addmap.getValue().getCount()==null){
				totalPrice+=addmap.getValue().getBook().getBookPrice();
				dangPrice+=addmap.getValue().getBook().getDangPrice();
			}else{
				totalPrice+=addmap.getValue().getBook().getBookPrice()*addmap.getValue().getCount();
				dangPrice+=addmap.getValue().getBook().getDangPrice()*addmap.getValue().getCount();
			}
			savePrice = totalPrice - dangPrice;
			cart.setSavePrice(savePrice);
			cart.setTotalPrice(dangPrice);
		}
		return cart;
	}
}
